const nodemailer = require("nodemailer")

let sendEmail = function(hosts, port, auto_user, auto_pass, icode, mail_from, mail_to, html) {
        // console.log(hosts, port, auto_user, auto_pass, icode, mail_from, mail_to, html);
        var transporter = nodemailer.createTransport({
            host: hosts,
            port: port,
            secure: true, // use SSL
            auth: {
                user: auto_user,
                pass: auto_pass
            }
        });


        var randomNum = '';
        for (var i = 0; i < icode; i++) { //几位数
            randomNum += Math.floor(Math.random() * 10);
        }


        if (html.length != 0) {
            var reg = /\$\{(.+?)\}/
            var reg_g = /\$\{(.+?)\}/g
            var result = html.match(reg_g)
            var list = []
            if (result !== null) {
                for (var i = 0; i < result.length; i++) {
                    var item = result[i]
                    list.push(item.match(reg)[1])
                }
                list[0] = randomNum;
            }
        }
        console.log(randomNum)
        var mailOptions = {
            from: mail_from, // sender address
            to: mail_to, // 发送给谁
            subject: '来自佳宇鸭后台的邮件', // 主题
            html: html.length != 0 ? html.replace("${randomNum}", randomNum) : `<div id="contentDiv" style="position:relative;font-size:14px;height:auto;padding:15px 15px 10px 15px;z-index:1;zoom:1;line-height:1.7;" class="body"><div id="qm_con_body"><div id="mailContentContainer"><div style="margin: -8px -8px 0; padding: 38px 0;background-color: rgb(240, 242, 245);"><table align="center" border="0" cellspacing="0" cellpadding="0" width="100%" bgcolor="#EDF0F3" style="height: 100px; table-layout: fixed; font-family: PingFangSC-Light,serif;"><tbody><tr><td><table width="100%" border="0" cellspacing="0" cellpadding="0" style="max-width: 600px; margin: 0 auto;"><tbody><tr><td align="center"><div><h2 style="text-decoration: none;font-size: 23px;font-family: verdana;color: #ff9c56;">佳宇鸭后台管理系统</h2></div></td></tr><tr><td><table border="0" width="100%" cellspacing="0" cellpadding="0" style="box-shadow: 0 1px 2px 0 rgba(0,0,0,0.20);color: #273444;background: #FFF;border-radius: 4px;margin-top: 18px;"><tbody><tr><td colspan="4" style="border-top-left-radius: 4px;border-top-right-radius: 4px;"><table border="0" width="100%" cellspacing="0" cellpadding="0"><tbody><tr><td style="border-top-left-radius: 4px;border-top-right-radius: 4px;"><table border="0" width="100%" cellspacing="0" cellpadding="0" style="width: 100%; background: #FFF;padding:40px;border-top-left-radius: 4px;border-top-right-radius: 4px;"><tbody><tr><td><div style="margin: 10px 0;font-size: 16px;">佳宇鸭后台管理系统邮箱验证</div></td></tr><tr><td width="100%"><div style="margin: 10px 0;font-size: 14px; line-height: 1.6;"><div></div></div></td></tr><tr><td><span style="margin:10px 0;border-bottom:1px solid #E0E6ED;width: 176px;height: 0;display: inline-block;"></span></td></tr><tr><td align="center"><div>验证码：<span style="color: #ff9c56;font-size: 14px;text-decoration: none;font-weight: 600;padding: 11px 20px;margin-top: 3px;display: inline-block;">${randomNum}</span></div></td></tr></tbody></table></td></tr><tr><td><table width="100%" style="background: #F9FAFC;padding: 16px  40px;"><tbody><tr style="vertical-align: middle;"><td align="center"><div style="margin:10px 0;border-bottom:1px solid #E0E6ED;width: 64px;height: 0;"></div></td></tr><tr style="vertical-align: middle;"><td align="center"><div style="vertical-align: middle;"><p style="font-size: 14px; color: #3C4858;; margin:0 0 5px 0;"> 感谢您对佳宇鸭的支持</p><p style="margin: 0; font-size: 12px; color: #8796A8;"> 如有疑问联系QQ：1280063347</p></div></td></tr></tbody></table></td></tr></tbody></table></td></tr><tr><td width="25%;" style="background: #3C4858;font-size: 12.6px;color: #CAD3DE;text-align: center;border-bottom-left-radius: 4px;"><span style="padding: 15px 0px;display: inline-block;"><a href="http://localhost:8080/third/mail" style="color: #CAD3DE; text-decoration: none;" rel="noopener" target="_blank">返回系统</a></span></td></tr></tbody></table></td></tr><tr style="font-size: 9px; color: #3C4858;"><td style="text-align: center; padding-top: 5px;"><span>佳宇鸭后台管理系统</span></td></tr></tbody></table></td></tr></tbody></table></div></div></div></div>`
        };
        return new Promise((resolve, reject) => {
            transporter.sendMail(mailOptions, function(error, info) {
                if (error) {
                    // return error;
                    resolve({
                        code: -3,
                        messages: "发送失败"
                    })
                } else {
                    resolve({
                        code: 200,
                        messages: "发送成功"
                    })
                }

                // console.log('消息已发送: ' + info.response);
                // return info.response

            });

        })
    }
    // sendEmail();
module.exports = {
    sendEmail: sendEmail
}